package cn.com.manager.controller.weibo;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.manager.service.WeiboService;

@Controller
@RequestMapping(value = "/weibo")
public class WeiBoController {
	@Autowired
	private WeiboService weiboService;

	@RequestMapping("/index")
	public String index(HttpServletRequest request, Model model) {
		String currentUserId = getCurrentUserId(request);
		if (StringUtils.isNoneBlank(currentUserId)) {
			setBaseInfo(currentUserId,model);
			return "weibo/home";
		}
		return "weibo/index";
	}

	/**
	 * 用户注册
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/register")
	public String register(String username, String password) {
		weiboService.saveUser(username, password);
		return "redirect:/weibo/index";
	}

	/**
	 * 用户登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username, String password, Model model, HttpServletResponse response) {
		String userId = weiboService.userLogin(username, password);
		String url = "";
		if (StringUtils.isNoneBlank(userId)) {
			Cookie cookie = new Cookie("weibo_currentUser", userId);
			response.addCookie(cookie);
			url = "redirect:/weibo/index";
		} else {
			model.addAttribute("loginInfo", "用户名或密码错误!");
			url = "weibo/index";
		}
		return url;
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if ("weibo_currentUser".equals(cookie.getName())) {
				cookie.setValue(null);
				response.addCookie(cookie);
				break;
			}
		}

		return "weibo/index";
	}

	/**
	 * 个人主页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/home")
	public String home(Model model) {
		return null;
	}

	@RequestMapping("/profile")
	public String profile(String username, HttpServletRequest request, Model model) {
		String currentUserId = getCurrentUserId(request);
		Map<String, Object> userInfo = weiboService.getUserInfo(username);
		model.addAttribute("isFollow", weiboService.isFollow(username, currentUserId));
		model.addAttribute("userInfo", userInfo);
		List<Map<String, Object>> post = weiboService.getPost(String.valueOf(userInfo.get("userId")));
		model.addAttribute("postInfos",post);
		return "/weibo/profile";
	}

	/**
	 * 关注
	 * 
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping("/follow")
	public String follow(String uid, String username, String isFollow, HttpServletRequest request, Model model) {
		String currentUserId = getCurrentUserId(request);
		if (StringUtils.isNotBlank(currentUserId)) {
			if ("0".equals(isFollow)) {
				weiboService.follow(uid, currentUserId);
			} else {
				weiboService.cancelFollow(uid, currentUserId);
			}
		}
		model.addAttribute("isFollow", weiboService.isFollow(username, currentUserId));
		model.addAttribute("userInfo", weiboService.getUserInfo(username));
		return "/weibo/profile";
	}

	/**
	 * 最新注册用户
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/timeline")
	public String timeline(Model model) {
		List<String> newRegisterUser = weiboService.newRegisterUser();
		model.addAttribute("newUserList", newRegisterUser);
		model.addAttribute("recentPosts",weiboService.getPost());
		return "weibo/timeline";
	}

	/**
	 * 发布微博
	 * 
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping("/publish")
	public String publish(String content, HttpServletRequest request, Model model) {
		String currentUserId = getCurrentUserId(request);
		weiboService.publish(currentUserId, content);
		return "redirect:/weibo/index";
	}

	private String getCurrentUserId(HttpServletRequest request) {
		String currentUserId = "";
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("weibo_currentUser".equals(cookie.getName())) {
					currentUserId = cookie.getValue();
				}
			}
		}
		return currentUserId;
	}
	
	private void setBaseInfo(String currentUserId,Model model){
		Map<String, Object> userInfo = weiboService.getUserInfoById(currentUserId);
		Map<String, Long> followInfo = weiboService.followInfoAmount(currentUserId);
		List<Map<String, Object>> post = weiboService.getPost(currentUserId);
		model.addAttribute("currentUser", userInfo.get("username"));
		model.addAttribute("followInfo", followInfo);
		model.addAttribute("postInfos",post);
	}
}
